package TCPdemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-10-11
 * Time: 23:04
 */
public class TCPechoclient {
    private Socket socket = null;

    public TCPechoclient(String server, int port) throws IOException {
        socket = new Socket(server,port);
    }
    public void start(){
        Scanner scan = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            while (true){
                System.out.print("-> ");
                String rsq = scan.next();
                PrintWriter writer = new PrintWriter(outputStream);
                writer.println(rsq);
                writer.flush();
                Scanner scanner = new Scanner(inputStream);
                String resp = scanner.next();
                System.out.println(resp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TCPechoclient client = new TCPechoclient("127.0.0.1", 9090);//
        client.start();
    }
}
